package com.andyliu.sys.service.impl;

import com.andyliu.sys.mapper.SDeptMapper;
import com.andyliu.sys.pojo.SDept;
import com.andyliu.sys.pojo.SDeptExample;
import com.andyliu.sys.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: DeptServiceImpl
 * Description: 部门业务
 * date: 2021/1/20 5:15 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private SDeptMapper mapper;
    @Override
    public Boolean saveDept(SDept dept) {
        int flag =0;
        if(dept!=null && dept.getId()!=null){
            flag = mapper.updateByPrimaryKeySelective(dept);
        }else{
            flag = mapper.insertSelective(dept);
        }
        return flag==1?true:false;
    }

    @Override
    public Boolean deleteDeptById(Integer id) {
        int flag = mapper.deleteByPrimaryKey(id);
        return flag==1?true:false;
    }

    @Override
    public SDept selectDeptById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SDept> selectDeptByPage(SDept dept, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SDeptExample ex = new SDeptExample();
        SDeptExample.Criteria criteria = ex.createCriteria();
        if(dept!=null){
            if(dept.getDeptname()!=null && !"".equals(dept.getDeptname())){
                criteria.andDeptnameLike("%"+dept.getDeptname()+"%");
            }
        }
        List<SDept> depts = mapper.selectByExample(ex);

        return new PageInfo<SDept>(depts);
    }

    @Override
    public Boolean deleteDeptById(Integer[] ids) {
        boolean status = false;
        int flag = mapper.deletes(ids);
        if(flag == ids.length){
            status = true;
        }
        return status;
    }

    @Override
    public List<SDept> queryDepts() {
        return mapper.selectByExample(null);
    }

    @Override
    public List<SDept> queryDeptParent() {
        SDeptExample example = new SDeptExample();
        SDeptExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        return mapper.selectByExample(example);
    }
}
